-- Funções de manipulação de Tokens

module Tokens
(
	tokenToString,
	stringToToken
) where

import Types
import Data.Char


-- Converte um Token para uma string
tokenToString :: Token -> String
tokenToString token = 
	case token of
		Program_tk -> "PROGRAM"
		Plus_tk -> "+"
		Identifier_tk token -> "ID " ++ (show token)
		End_tk -> "END"
		Integer_tk token -> "INTEGER " ++ (show token)
		Character_tk token -> "CHARACTER " ++ (show token)
		String_tk n token -> "CHARACTER[" ++ (show n) ++ "] " ++ token
		Logical_tk token -> "LOGICAL " ++ (show token)
		Real_tk token -> "REAL " ++ (show token)
		Type_tk token -> "TYPE " ++ token
		Define_tk -> "DEFINE"
		True_tk -> "TRUE"
		False_tk -> "FALSE"
		And_tk -> "AND"
		Or_tk -> "OR"
		Eqv_tk -> "EQV"
		Neqv_tk -> "NEQV"
		Read_tk -> "READ"
		Write_tk -> "WRITE"
		If_tk -> "IF"
		While_tk -> "WHILE"
		Then_tk -> "THEN"
		Endif_tk -> "ENDIF"
		Else_tk -> "ELSE"
		Default_tk -> "DEFAULT"
		Do_tk -> "DO"
		Select_tk -> "SELECT"
		Case_tk -> "CASE"
		Subroutine_tk -> "SUBROUTINE"
		Function_tk -> "FUNCTION"
		ParentesesL_tk -> "("
		ParentesesR_tk -> ")"
		ContLinha_tk -> "&"
		Equals_tk -> "="
		Minus_tk -> "-"
		Exponential_tk -> "**"
		Division_tk -> "/"
		Multiplication_tk -> "*"
		Point_tk -> "."
		MaiorQue_tk -> ">"
		MenorQue_tk -> "<"
		MaiorIgualQue_tk -> ">="
		MenorIgualQue_tk -> "<="
		Diferent_tk -> "/="
		IgualA_tk -> "=="
		Concat_tk -> "+."
		Virgula_tk -> ","
		PontoVirgula_tk -> ";"
		ColcheteL_tk -> "["
		ColcheteR_tk -> "]"
		Close_tk -> "CLOSE"
		Rewind_tk -> "REWIND"
		EndFile_tk -> "ENDFILE"
		Format_tk -> "FMT"
		Recursive_tk -> "RECURSIVE"
		Contains_tk -> "CONTAINS"
		Error_tk -> "ERROR"

-- Converte uma string para Token
stringToToken :: String -> Token
stringToToken str =
	case str of
		"PROGRAM" -> Program_tk
		"+" -> Plus_tk
		"END" -> End_tk
		"INTEGER" -> Integer_tk 0
		"LOGICAL" -> Logical_tk False
		"REAL" -> Real_tk 0
		"CHARACTER" -> Character_tk '\0'
		"TYPE" -> Type_tk ""
		"DEFINE" -> Define_tk
		"TRUE" -> True_tk
		"FALSE" -> False_tk
		"AND" -> And_tk 
		"OR" -> Or_tk 
		"EQV" -> Eqv_tk
		"NEQV" -> Neqv_tk
		"READ" -> Read_tk
		"WRITE" -> Write_tk 
		"IF" -> If_tk
		"WHILE" -> While_tk
		"THEN" -> Then_tk
		"ENDIF" -> Endif_tk
		"ELSE" -> Else_tk
		"DEFAULT" -> Default_tk
		"DO" -> Do_tk
		"SELECT" -> Select_tk
		"CASE" -> Case_tk
		"SUBROUTINE" -> Subroutine_tk
		"FUNCTION" -> Function_tk
		"(" -> ParentesesL_tk
		")" -> ParentesesR_tk
		"&" -> ContLinha_tk
		"=" -> Equals_tk
		"-" -> Minus_tk
		"**" -> Exponential_tk
		"/" -> Division_tk
		"*" -> Multiplication_tk
		"." -> Point_tk
		">" -> MaiorQue_tk
		"<" -> MenorQue_tk
		">=" -> MaiorIgualQue_tk
		"<=" -> MenorIgualQue_tk
		"/=" -> Diferent_tk
		"==" -> IgualA_tk
		"+." -> Concat_tk
		"," -> Virgula_tk
		";" -> PontoVirgula_tk
		"[" -> ColcheteL_tk
		"]" -> ColcheteR_tk
		"CLOSE" -> Close_tk
		"REWIND" -> Rewind_tk
		"ENDFILE" -> EndFile_tk
		"FMT" -> Format_tk
		"RECURSIVE" -> Recursive_tk
		"CONTAINS" -> Contains_tk
		_ -> Identifier_tk str

